

Component({
    behaviors: [],
    properties: {
        'food': Object
    },
    data: {
        num: 0,
        strogeFoods: wx.getStorageSync('foods')
    },
    ready: function(){
        var that = this
        setTimeout(function() {
            if(!that.data.food.count){
                that.setData({
                    'food.count':0
                })
            }
        },0)
        
    },
    methods: {
        shopAdd(e) {

            if(!this.data.food.count){
                this.setData({
                    'food.count':0
                })
            }
            this.data.food.count++;
            this.setData({
                'food.count': this.data.food.count
            })
            //console.log(this.data.food)
            this.triggerEvent('foodMess', {food:this.data.food},{ bubbles: true })
            wx.setStorageSync("foods",this.setFoods(this.data.food))
        },
        shopDec() {
            this.data.food.count--;
            if(this.data.food.count <= 0){
                this.data.food.count=0;
            }
            this.setData({
              //输出动画
              'food.count': this.data.food.count
            })
            this.triggerEvent('foodMess', {food:this.data.food},{ bubbles: true })
            wx.setStorageSync("foods",this.setFoods(this.data.food))
        },

        setFoods(food) {
            let strogeFoods = wx.getStorageSync('foods');
            let flag = true;
            let specFlag = true;
            if(strogeFoods == ''){
                strogeFoods = []
                strogeFoods.push(food);
            }else{
                //如果多规格
                if(food.spec){
                    
                    for(let i=0;i<strogeFoods.length;i++) {
                        if(strogeFoods[i].id == food.id){
                            strogeFoods[i].spec.forEach( (val1,i) => {
                                for(let j=0;j<food['spec'].length;j++){
                                    if(i==j){
                                        console.log(food['spec'][j])
                                        if(val1.spec_name != food['spec'][j].spec_name){
                                            specFlag = false;
                                            return false
                                        }
                                    }
                                }
                            })
                            if(specFlag){
                                strogeFoods[i] = food;
                                break;
                            }else {
                                specFlag = true;
                            }
                        }
                    }
                }else{
                    for(let i=0;i<strogeFoods.length;i++){
                        if(strogeFoods[i].id == food.id){
                            flag = false;
                            strogeFoods[i] = food
                        }
                    }
                    if(flag){
                        strogeFoods.push(food);
                    }
                }
                
            }
            for(let i=0;i<strogeFoods.length;i++){
                if(strogeFoods[i].count == 0){
                    strogeFoods.splice(i,1)
                }
            }
            console.log(strogeFoods)
            
            return strogeFoods
        }
    }
})

